﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class YGRVKDOC : DocumentBase
    {

        #region=============Properties=======================
        //TBList="ZUTEI;VVPACK;GPACK;VVTLIEF;VVLIEF;VKADR;ICC_WFEVENTDEF;KUNDADR;SDDAT;VKPOS;VKKOPF;WORKFLOWITX;WAPROF;QPZUORD;QPPLAN;ICC_QPSTEP;HRSTAMM;VAFARBEN;VAART;TXTKON;TXTIND;ZABED;ICC_WFEVENTS;KDSTAMM;KDANSP;ICC_QPSTEP;ICC_TXTKON;";//YGRVKOPS
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        string Mandant = "00";
        bool IsSetUp = false;
        YGRVKDOCData m_Data;

        public DataSet setResult;
        ITXEXRPT ITXEXRPT;

        #region==========CollectionViewSource=================
        SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetData");
            }
        }
        SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetEnv");
            }
        }
        SETINTEXDB SetVV
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetVV");
            }
        }
        SETINTEXDB SetDetail
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetDetail");
            }
        }

        CollectionViewSource vwMaster
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwMaster");
            }
        }
        DataRow dwMaster
        {
            get
            {
                if (vwMaster.View == null)
                    return null;
                if (vwMaster.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMaster.View.CurrentItem).Row;
            }
        }  
        string ART
        {
            get
            {
                if (dwMaster == null)
                    return "";
                return dwMaster["VKPART"].ToString();

            }
        }
        public string KRS
        {
            get
            {
                return m_Data.Right("  " + txtVKPAUFKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtVKPAUFKRS.Text = value;
            }
        }
        public string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtVKPAUFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtVKPAUFLFD.Text = value;
            }
        } 
        public string POS
        {
            get
            {
                return m_Data.Right("000000" + txtVKPAUFPOS.Text.ToString().Trim(), 3); ;
            }
            set
            {
                txtVKPAUFPOS.Text = value;
            }
        }
        #endregion
        BindingListCollectionView cvMaster
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgMaster.ItemsSource);
            }
        }
        BindingListCollectionView cvVVT
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgVVT .ItemsSource);
            }
        }
        BindingListCollectionView cvGPACK
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgPACK.ItemsSource);
            }
        }
        BindingListCollectionView cvZUTEI
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgZUTEI.ItemsSource);
            }
        }
        #endregion
        int total = 0;
        public YGRVKDOC()
        {
            InitializeComponent();
            this.Name = "销售合同";
        } 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                SetEnv.Clear();
                SetEnv.Merge(m_Data.LoadEnv());
                IsLoaded = true;
                btnQuery.IsEnabled = false;
                vwMaster.View.CurrentChanged += vwMaster_CurrentChanged;
                vwEvents.View.CurrentChanged += vwEvents_CurrentChanged;
                vwVVLIEF.View.CurrentChanged += vwVVLIEF_CurrentChanged;
                if (app.User == "ITXV    ")
                {
                    btnQuery_Click(null, null);
                    //btnQueryVV_Click(null, null); 
                }
              
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }

        void vwVVLIEF_CurrentChanged(object sender, EventArgs e)
        {
            try
             {
                 if (dwVVLIEF == null)
                 {
                     cvVVT.CustomFilter = null;
                     cvGPACK.CustomFilter = null; 
                     return;
                 }
                 string filter = "VVTLIEFSCH ='" + dwVVLIEF["VVLIEFSCH"].ToString() + "'";
                 cvVVT.CustomFilter = filter;
                 filter = "GPALIEFSCH ='" + dwVVLIEF["VVLIEFSCH"].ToString() + "'";                
                cvGPACK.CustomFilter = filter;               
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                string filter = "TXTIPIND ='" + dwMaster["VKPAUFKRS"].ToString() + dwMaster["VKPAUFLFD"].ToString() + "000IVDI                                             '";
                DataRow[] rows = SetData.ICC_TXTKON.Select(filter);
                if (rows.Length > 0)
                {
                    DataTable tb = rows.CopyToDataTable();
                    tb.TableName = "ICC_TXTKON";
                    SetDetail.ICC_TXTKON.Clear();
                    SetDetail.Merge(tb);
                    if (vwPTXT.View != null)
                    {
                        vwPTXT.View.MoveCurrentToFirst();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent();
                }
                m_Data = new YGRVKDOCData();
                ITXEXRPT = new ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel";
                txtUserID.Text = m_Data.app.User;
                txtPassword.Password = m_Data.app.Pwd;
                btnQuality.IsEnabled = false;
                btnMaterial.IsEnabled = false;
                btnColor.IsEnabled = false;
                btnPlan.IsEnabled = false;
                btnProdd.IsEnabled = false;
                btnSales.IsEnabled = false;
                btnVV.IsEnabled = false;
                IsColor.IsEnabled = false;
                IsQuality.IsEnabled = false;
                IsProd.IsEnabled = false;
                IsMaterial.IsEnabled = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(SPassword);
            string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            DataRow dwHR = app.UserValid(userID, Password);
            if (dwHR == null)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            expLog.IsExpanded = false;
            btnQuery.IsEnabled = true;
        }

        private void btnLoadApp_Click(object sender, RoutedEventArgs e) 
        {
            //VKPKOAUFKRS,VKPKOAUFLFD,VKPKOAUFPOS,VKPKOAUFART
            EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
            DataSet SResult = new DataSet();
            btnStartApp.IsEnabled = true;
            btnStartApp.Content = "送审";
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    SResult = m_Data.BuildData("00", dwMaster["VKPKOAUFKRS"].ToString(), dwMaster["VKPKOAUFLFD"].ToString());
                    SetData.Clear();
                    SetData.Merge(SResult);              
                    if (SetData.ICC_WFEVENTS.Rows.Count > 0)
                    {
                        btnStartApp.IsEnabled = false;
                        btnStartApp.Content = "已启动评审";
                    }
                    string filter = "VKPAUFART='NA'";
                    DataRow[] rows = SetData.Tables["VKPOS"].Select(filter);
                    if (rows.Length ==0)
                    {
                        btnStartApp.IsEnabled = false;
                        btnStartApp.Content = "已评审或无需评审";
                    }
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                    if (vwVKKOPF.View != null)
                    {
                        vwVKKOPF.View.MoveCurrentToFirst();
                    }
                    if (dwVKKOPF == null)
                        return;
                    var qz = from dwTemp in SetData.ICC_TXTKON.AsEnumerable()
                             where dwTemp["TXTIPIND"].ToString().Trim() == dwVKKOPF["VKKZAHLBED"].ToString().Trim() + "V"
                             select dwTemp;
                    foreach (DataRow row in qz)
                    {
                        txtPayment.Text=Helpers.Helper.GetGB2312Value(row["TXT010"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                try
                {
                    SetData.Clear();
                    SetData.Merge(SResult);
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                    if (vwVKKOPF.View != null)
                    {
                        vwVKKOPF.View.MoveCurrentToFirst();
                    }
                }
                catch (Exception x)
                { }
            }
            EventDispatcher.Instance.RaiseStatus("查询结束", StatusEventType.StopProgress);
        } 
       
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            //VKPKOAUFKRS,VKPKOAUFLFD,VKPKOAUFPOS,VKPKOAUFART
            EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
            DataSet SResult = new DataSet();
            btnStartApp.IsEnabled = true;
            btnStartApp.Content = "送审";
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    SResult = m_Data.BuildData("00",KRS,LFD);
                    SetData.Clear();
                    SetData.Merge(SResult);
                    SetDetail.Clear();
                    if (SetData.ICC_WFEVENTS.Rows.Count > 0)
                    {
                        btnStartApp.IsEnabled = false;
                        btnStartApp.Content = "已启动评审";
                    }
                    string filter = "VKPAUFART='AN'";
                    DataRow[] rows = SetData.Tables["VKPOS"].Select(filter);
                    if (rows.Length ==0)
                    {
                        btnStartApp.IsEnabled = false;
                        btnStartApp.Content = "已评审或无需评审";
                    }
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                    if (vwVKKOPF.View != null)
                    {
                        vwVKKOPF.View.MoveCurrentToFirst();
                    }
                    if (dwVKKOPF == null)
                        return;
                    var qz = from dwTemp in SetData.ICC_TXTKON.AsEnumerable()
                             where dwTemp["TXTIPIND"].ToString().Trim() == dwVKKOPF["VKKZAHLBED"].ToString().Trim() + "V"
                             select dwTemp;
                    foreach (DataRow row in qz)
                    {
                        txtPayment.Text=Helpers.Helper.GetGB2312Value(row["TXT010"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                try
                {
                    SetData.Clear();
                    SetData.Merge(SResult);
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                    if (vwVKKOPF.View != null)
                    {
                        vwVKKOPF.View.MoveCurrentToFirst();
                    }
                }
                catch (Exception x)
                { }
            }
            EventDispatcher.Instance.RaiseStatus("查询结束", StatusEventType.StopProgress);
        } 
        private void txtVKPAUFLFD_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter)
            {
                return;
            }
            btnQuery_Click(null, null);
        }

        private void btnPrintVKDEK_Click(object sender, RoutedEventArgs e)
        { 
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.dsSource.Merge(SetEnv);
    
            ITXEXRPT.DocType = app.company+"VKKDEK";
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        private void btnPrintVKPOS_Click(object sender, RoutedEventArgs e)
        {            
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.dsSource.Merge(SetEnv);         
            ITXEXRPT.DocType = app.company+"VKPOS";             
            ITXEXRPT.strExcelFile = app.company+@"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        private void btnPrintVKEX_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.dsSource.Merge(SetEnv);           
            ITXEXRPT.DocType = app.company+"VKEX";            
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }
        private void btnPrintVKEXVT_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = app.company + "VKEXVT";            
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        private void btnPrintLabDip_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = app.company + "LABDIPAPP";            
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport(); 
        }

        private void btnPaymentApp_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = app.company + "PAYMENTAPP";
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport(); 
        }

        private void btnQueryVV_Click(object sender, RoutedEventArgs e)
        {
            DataSet SResult = new DataSet();
            try
            {
              
                    SResult = m_Data.GetVV("00",KRS,LFD);
                    SetVV.Clear();
                    SetVV.Merge(SResult);
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                    if (vwVKKOPF.View != null)
                    {
                        vwVKKOPF.View.MoveCurrentToFirst();
                    }
                    if (dwVKKOPF == null)
                        return;
                    var qz = from dwTemp in SetData.ICC_TXTKON.AsEnumerable()
                             where dwTemp["TXTIPIND"].ToString().Trim() == dwVKKOPF["VKKZAHLBED"].ToString().Trim() + "V"
                             select dwTemp;
                    foreach (DataRow row in qz)
                    {
                        txtPayment.Text=row["TXT010"].ToString();
                    }
             
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
           
        }

        private void btnPrintVVVK_Click(object sender, RoutedEventArgs e)
        {
            if (dwVVLIEF == null)
            {
                MessageBox.Show("请选择发货单");
                return;
            }
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.dsSource.Merge(SetVV);
            DataRow[] rows = ITXEXRPT.dsSource.Tables["VVLIEF"].Select("VVLIEFSCH<>'" + dwVVLIEF["VVLIEFSCH"].ToString()+"'");
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            ITXEXRPT.dsSource.Tables["VVLIEF"].AcceptChanges();
            ITXEXRPT.DocType = app.company + "VVVK";
            ITXEXRPT.strExcelFile = app.company + @"\YGRVKPOS.xls";
            string filename = ITXEXRPT.WriteExcelReport(); 
        }
 

       
    }
}
